﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Admin.RelationQuery
{
    public partial class ComapnyUpdateAreaDeatil : BasePage
    {

        public ComapnyUpdateAreaDeatil()
        {
            this.ModuleNo = Module.配送关系管理.ToString();
            this.PageOperateType = OperateType.配送区域新增.ToString();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                Bind();
            }
        }
        public void Bind()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["Companyid"]))
            {
                lblcompanyid.Text = Request.QueryString["Companyid"].ToString();
            }
            if (!string.IsNullOrEmpty(Request.QueryString["Companyname"]))
            {
                lblcompanyname.Text = Request.QueryString["Companyname"].ToString();
            }
            string userid = Request.QueryString["Companyid"].ToString();//配送企业的id
            IList<Area> areaList = HZ.DrugTrade.BLLFactory.Factory.BLL_Area().GetUnChoseAreaByZBZX(userid);
            this.gvwArea.DataSource = areaList;
            this.gvwArea.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            //获取所有选择的地区id
            List<string> areaIds = new List<string>();


            //遍历列表的所有行
            for (int _index = 0; _index < gvwArea.Rows.Count; _index++)
            {
                CheckBox ckb = ((CheckBox)gvwArea.Rows[_index].FindControl("checkbox1"));
                if (ckb.Checked)
                {
                    areaIds.Add(this.gvwArea.DataKeys[_index].Value.ToString());
                }
            }

            if (areaIds.Count > 0)
            {
                string userid = Request.QueryString["Companyid"].ToString();
                bool flag = HZ.DrugTrade.BLLFactory.Factory.BLL_Area().UpdateComapnyAreaByCompanyid(areaIds, userid);
                if (flag)
                {
                    this.Controls.Add(new LiteralControl("<script>alert('选择成功！');window.location.href='ComapnyUpdateArea.aspx';</script>"));
                }
                else
                {
                    this.Controls.Add(new LiteralControl("<script>alert('选择失败或该地区已被选择！');</script>"));
                }
            }
            else
            {
                HZ.Web.WebUtil.Alert("请至少选定一个配送区域！", this);
            }
        }

        protected void HzButton1_Click(object sender, EventArgs e)
        {
            Response.Redirect("ComapnyUpdateArea.aspx");
        }
    }
}